package com.benmei.weike.web;

import com.benmei.weike.common.Constants;
import com.benmei.weike.dto.FindCommentsPageRequest;
import com.benmei.weike.dto.PageResponse;
import com.benmei.weike.dto.mcCourse.McCourseDetailResponse;
import com.benmei.weike.dto.weikeContentResponse.StudentWeikeContentRespons;
import com.benmei.weike.dto.weikeDetailResponse.WeikeDetailResponse;
import com.benmei.weike.entity.WeikeContent;
import com.benmei.weike.service.McCourseStudentService;
import com.benmei.weike.service.WeikeCommentsService;
import com.benmei.weike.service.WeikeService;
import com.benmei.weike.util.JsonUtil;
import com.benmei.weike.web.vo.mcCourse.McCourseGroupBuyResultResponse;
import com.nativetalk.base.RetInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Peter on 2017/5/25.
 */
@Controller
@RequestMapping(value = "/html/share")
public class WeikeShareController {
    public static final Logger logger = LoggerFactory.getLogger(WeikeShareController.class);

    //log 记录方法入惨的前缀
    private static final String prefix_in = "\nparameters in -->> ";
    //log 记录方法出惨的前缀
    private static final String prefix_out = "\nparameters out -->>";

    @Autowired
    private WeikeService weikeService;

    @Autowired
    private WeikeCommentsService weikeCommentsService;
    @Autowired
    private McCourseStudentService mcCourseStudentService;

    /**
     * 微课详情
     * 页面与app页面的区别：
     * 页面无法获取用户信息
     * 1.学生个人评分无法获取
     * 2.购买微课无法实现
     * 3.
     *
     * @param weikeId
     * @param model
     * @return
     */
    @RequestMapping(value = "/detail/{weikeId}")
    public String detail(@PathVariable("weikeId") Long weikeId, Model model) {
        logger.info(prefix_in + "weikeId:" + weikeId);
        try {
            //查询微课详情
            WeikeDetailResponse weikeDetail = weikeService.getWeikeDetailForSharePage(weikeId);

            //查询微课评论
            FindCommentsPageRequest req = new FindCommentsPageRequest();
            req.setWeike_id(weikeId);
            PageResponse comments = weikeCommentsService.studentFindComments(req);

            model.addAttribute("weikeDetail", weikeDetail);
            model.addAttribute("comments", comments);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "weike-detail";
    }

    /**
     * 微课详情
     * 页面与app页面的区别：
     * 页面无法获取用户信息
     * 1.学生个人评分无法获取
     * 2.购买微课无法实现
     * 3.
     *
     * @param weikeId
     * @param model
     * @return
     */
    @RequestMapping(value = "/content/{weikeId}")
    public String content(@PathVariable("weikeId") Long weikeId, Model model) {
        logger.info(prefix_in + "weikeId:" + weikeId);
        try {
            StudentWeikeContentRespons weikeContent = weikeService.getWeikeContent(null, weikeId);

            model.addAttribute("weikeContent", weikeContent);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "weike-content";
    }


    @RequestMapping(value = "/createWeikeHelp")
    public String createWeikeHelp(Model model) {
        return "createWeikeHelp";
    }

    @RequestMapping(value = "/toUpdateOssFileNamePage/{weikeId}")
    public String toUpdateOssFileNamePage(@PathVariable("weikeId") Long weikeId, Model model) {
        logger.info(prefix_in + "weikeId:" + weikeId);
        try {
            //查询微课详情
            List<WeikeContent> list = weikeService.getWeikeAudio(weikeId);

            model.addAttribute("list", list);
            model.addAttribute("count", list.size());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "weike-update-ossFileName";
    }

    /**
     * 跳转到分享页面
     *
     * @param invite_code
     * @return
     */
    @RequestMapping(value = "/register", method = {RequestMethod.POST, RequestMethod.GET})
    public String toShareRegist(@RequestParam(value = "invite_code", required = false) String invite_code, Model model) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + "invite_code=" + invite_code);

            model.addAttribute("invite_code", invite_code);

            //打印出参
            logger.info(Constants.prefix_out + "invite_code=" + invite_code);
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
        }
        return "register";
    }

    // ----------------------------- 系统课 -----------------------------------------------------------------------------------------
    // to app里面看到的拼团结果页面
    @RequestMapping(value = "/mcCourse/{memb_id}/{mc_course_group_id}")
    public String mcCourseGroupBuyResult(@PathVariable("memb_id") Integer memb_id, @PathVariable("mc_course_group_id") Integer mc_course_group_id, Model model,
                                         @RequestHeader(value = "AppType", required = false, defaultValue = "A") String app_type) {
        logger.info(prefix_in + "memb_id:" + memb_id + ",mc_course_group_id:" + mc_course_group_id + ",app_type:" + app_type);
        try {
            if(StringUtils.isBlank(app_type)){
                app_type=Constants.AppType.A;
            }

            McCourseGroupBuyResultResponse resultResponse = mcCourseStudentService.findGroupBuyResult(mc_course_group_id);
            logger.info(prefix_out + JsonUtil.toJson(resultResponse));
            model.addAttribute("resultResponse", resultResponse);
            model.addAttribute("app_type", app_type);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "mcCourse/group-buy-result";
    }

    // to app微信拼团页面
    @RequestMapping(value = "/mcCourse/weixinPage/invitation/{memb_id}/{mc_course_group_id}")
    public String mcCourseGroupWeixinPageInvitation(@PathVariable("memb_id") Integer memb_id, @PathVariable("mc_course_group_id") Integer mc_course_group_id, Model model) {
        logger.info(prefix_in + "memb_id:" + memb_id + ",mc_course_group_id:" + mc_course_group_id);
        try {
            McCourseGroupBuyResultResponse resultResponse = mcCourseStudentService.findGroupBuyResult(mc_course_group_id);
            logger.info(prefix_out + JsonUtil.toJson(resultResponse));
            model.addAttribute("resultResponse", resultResponse);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "mcCourse/weixinPage-join-group";
    }


    //to h5 大班课课程详情
    @RequestMapping(value = "/mcCourse/weixinPage/detail/{mcCourseId}")
    public String mcCourseGroupWeixinPageInvitation(@PathVariable("mcCourseId") Integer mcCourseId, Model model) {
        logger.info(prefix_in + "mcCourseId:" + mcCourseId );
        try {
            McCourseDetailResponse response = mcCourseStudentService.mcCourseDetail(mcCourseId, null);
            model.addAttribute("detail", response);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "mcCourse/weixinPage-mcCourseDetail";
    }

}
